var WebSocketService = (function () {
    function t() {
      (this.listeners = []),
        (this.queue = []),
        (this.socket = new Laya.Socket()),
        this.socket.on(Laya.Event.OPEN, this, this.openHandler),
        this.socket.on(Laya.Event.MESSAGE, this, this.receiveHandler),
        this.socket.on(Laya.Event.CLOSE, this, this.closeHandler),
        this.socket.on(Laya.Event.ERROR, this, this.errorHandler);
    }
    return (
      (t.open = function (e) {
        null == this.instance && (this.instance = new t()),
          this.instance.socket.connected ||
            (this.instance.socket.connectByUrl(e),
            (this.instance.url = e),
            console.log("timeout:" + this.instance.socket.timeout));
      }),
      (t.prototype.reOpen = function () {
        this.socket.connected
          ? console.log(this.socket.connected)
          : (this.socket.connectByUrl(this.url), SelectRoleView.open());
      }),
      (t.send = function (t) {
        return (
          null != this.instance &&
          null != this.instance.socket &&
          (this.instance.socket.connected
            ? (Environment.production ||
                console.log("发送的数据:" + JSON.stringify(t)),
              this.instance.socket.send(JSON.stringify(t)),
              !0)
            : (this.instance.queue.push(t), !1))
        );
      }),
      (t.addListener = function (e, n) {
        void 0 === n && (n = !1),
          null == this.instance && (this.instance = new t());
        var i = this.instance.listeners;
        if (n) {
          var s = [];
          i.forEach(function (t, n) {
            t.code == e.code && s.push(n);
          }),
            s.forEach(function (t) {
              i.splice(t, 1);
            });
        }
        i.push(e);
      }),
      (t.prototype.openHandler = function (e) {
        t.addListener({
          caller: this,
          code: 15,
          method: function (t) {
            ToastView.toastView(t.text);
          },
        }),
          this.flush();
      }),
      (t.prototype.closeHandler = function (t) {
        this.isClose ||
          Alert.openCall("连接已关闭，是否重新连接？", {
            caller: this,
            method: this.reOpen,
            code: 0,
          });
      }),
      (t.close = function () {
        if (null != this.instance) {
          var t = this.instance.socket;
          null != t && t.connected && ((this.instance.isClose = !0), t.close());
        }
      }),
      (t.prototype.errorHandler = function (t) {
        void 0 === t && (t = null),
          ToastView.toastView("连接出错"),
          console.log("连接出错");
      }),
      (t.prototype.receiveHandler = function (t) {
        if (
          (void 0 === t && (t = null),
          Environment.production || console.log("接收到数据:" + t),
          t && "{" == t.charAt(0))
        ) {
          var e = JSON.parse(t);
          e.act_id &&
            this.listeners.forEach(function (t) {
              t.code == e.act_id && t.caller && t.method.call(t.caller, e);
            });
        }
      }),
      (t.prototype.flush = function () {
        for (; this.queue.length > 0; ) {
          var e = this.queue.shift();
          t.send(e);
        }
      }),
      (t.instance = null),
      t
    );
  })(),
  Msg = (function () {
    return function () {};
  })(),
  Listener = (function () {
    return function () {};
  })();
